package com.code.generator.model;

import com.code.generator.model.GeneratorConfiguration;
import com.code.generator.model.TableInfo;
import com.code.util.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

public class GeneratorConfigurationBuilder {
    private static final String DEFAULT_CONFIG = "generator/generator-config.properties";
    private static final String USER_CONFIG = ".generator/generator-config.properties";
    private static final String USER_CONFIG_TABLE = ".generator/tables/";

    public GeneratorConfigurationBuilder() {
    }

    public static GeneratorConfiguration build() {
        try {
            Properties e = new Properties();
            Object is = null;

            try {
                File userConfigFile = new File(FilenameUtils.concat(System.getProperty("user.home"), ".generator/generator-config.properties"));
                if(!userConfigFile.exists()) {
                    System.out.println("user custom generator config not exists, please confirm file exists : " + userConfigFile.getAbsolutePath());
                } else {
                    try {
                        is = new FileInputStream(userConfigFile);
                        e.load((InputStream)is);
                    } finally {
                        IOUtils.closeQuietly((InputStream)is);
                    }
                }
            } finally {
                if(is != null) {
                    IOUtils.closeQuietly((InputStream)is);
                }

            }

            if(e.isEmpty()) {
                try {
                    is = Thread.currentThread().getContextClassLoader().getResourceAsStream("generator/generator-config.properties");
                    e.load((InputStream)is);
                } finally {
                    if(is != null) {
                        IOUtils.closeQuietly((InputStream)is);
                    }

                }
            }

            return build(e);
        } catch (Exception var20) {
            throw new RuntimeException("load generator config error", var20);
        }
    }

    private static GeneratorConfiguration build(Properties properties) {
        GeneratorConfiguration daoConfiguration = new GeneratorConfiguration();
        daoConfiguration.setRootProjectDirectory(properties.getProperty("root.project.directory"));
        daoConfiguration.setDbUrl(properties.getProperty("db.url"));
        daoConfiguration.setDbUsername(properties.getProperty("db.username"));
        daoConfiguration.setDbPassword(properties.getProperty("db.password"));
        String excludeTables = properties.getProperty("exclude.tables");
        if(StringUtils.isNotBlank(excludeTables)) {
            daoConfiguration.setExcludeTables(Arrays.asList(StringUtils.split(excludeTables, daoConfiguration.getSplitChar())));
        }

        daoConfiguration.setProjectName(properties.getProperty("root.project.name"));
        daoConfiguration.setDomainModuleName(properties.getProperty("domain.module.name"));
        daoConfiguration.setDaoModuleName(properties.getProperty("dao.module.name"));
        daoConfiguration.setManagerModuleName(properties.getProperty("manager.module.name"));
        daoConfiguration.setServiceModuleName(properties.getProperty("service.module.name"));
        daoConfiguration.setControllerModuleName(properties.getProperty("controller.module.name"));
        daoConfiguration.setSafModuleName(properties.getProperty("saf.module.name"));
        daoConfiguration.setGenerateDomain(Boolean.valueOf(properties.getProperty("generate.domain")).booleanValue());
        daoConfiguration.setGenerateSqlMap(Boolean.valueOf(properties.getProperty("generate.sqlmap")).booleanValue());
        daoConfiguration.setGenerateDao(Boolean.valueOf(properties.getProperty("generate.dao")).booleanValue());
        daoConfiguration.setGenerateManager(Boolean.valueOf(properties.getProperty("generate.manager")).booleanValue());
        daoConfiguration.setGenerateService(Boolean.valueOf(properties.getProperty("generate.service")).booleanValue());
        daoConfiguration.setGenerateSafService(Boolean.valueOf(properties.getProperty("generate.saf")).booleanValue());
        daoConfiguration.setGenerateController(Boolean.valueOf(properties.getProperty("generate.controller")).booleanValue());
        daoConfiguration.setGenerateCreate(Boolean.valueOf(properties.getProperty("generate.create")).booleanValue());
        daoConfiguration.setGenerateUpdate(Boolean.valueOf(properties.getProperty("generate.update")).booleanValue());
        daoConfiguration.setGenerateDelete(Boolean.valueOf(properties.getProperty("generate.delete")).booleanValue());
        daoConfiguration.setGenerateQuery(Boolean.valueOf(properties.getProperty("generate.query")).booleanValue());
        daoConfiguration.setNeedInterface(Boolean.valueOf(properties.getProperty("generate.interface")).booleanValue());
        daoConfiguration.setOnExistsOverride(Boolean.valueOf(properties.getProperty("generate.on.exists.override")).booleanValue());
        daoConfiguration.setBasePackage(properties.getProperty("base.package.name"));
        daoConfiguration.setPersistenceType(properties.getProperty("generate.persistence.type"));
        daoConfiguration.setEncoding(properties.getProperty("generate.encoding"));
        daoConfiguration.setExcludeColumns(Arrays.asList(StringUtils.split(properties.getProperty("exclude.columns"), daoConfiguration.getSplitChar())));
        daoConfiguration.setUseMysql(Boolean.valueOf(properties.getProperty("generate.useMysql")).booleanValue());
        daoConfiguration.setUseOracle(Boolean.valueOf(properties.getProperty("generate.useOracle")).booleanValue());
        daoConfiguration.setUseRedis(Boolean.valueOf(properties.getProperty("generate.useRedis")).booleanValue());
        daoConfiguration.setUseSafServer(Boolean.valueOf(properties.getProperty("generate.useSafServer")).booleanValue());
        daoConfiguration.setUseSafClient(Boolean.valueOf(properties.getProperty("generate.useSafClient")).booleanValue());
        daoConfiguration.setUseZookeeper(Boolean.valueOf(properties.getProperty("generate.useZookeeper")).booleanValue());
        daoConfiguration.setUseMQ(Boolean.valueOf(properties.getProperty("generate.useMQ")).booleanValue());
        daoConfiguration.setNeedBaseUser(Boolean.valueOf(properties.getProperty("generate.needBaseUser")).booleanValue());
        daoConfiguration.setUseRpcModule(Boolean.valueOf(properties.getProperty("generate.useRpcModule")).booleanValue());
        daoConfiguration.setUseIsRpcModule(Boolean.valueOf(properties.getProperty("generate.useIsRpcModule")).booleanValue());
        daoConfiguration.setUseWorkModule(Boolean.valueOf(properties.getProperty("generate.useWorkModule")).booleanValue());
        return daoConfiguration;
    }

    public static List<TableInfo> buildTableInfo(List<TableInfo> tableInfoList) {
        TableInfo tableInfo;
        if(tableInfoList != null) {
            for(Iterator var2 = tableInfoList.iterator(); var2.hasNext(); tableInfo = buildTableInfo(tableInfo)) {
                tableInfo = (TableInfo)var2.next();
            }
        }

        return tableInfoList;
    }

    public static TableInfo buildTableInfo(TableInfo tableInfo) {
        Properties properties = new Properties();
        FileInputStream is = null;

        try {
            File userConfigFile = new File(FilenameUtils.concat(System.getProperty("user.home"), ".generator/tables/" + tableInfo.getTableName()));
            System.out.println(System.getProperty("user.home"));
            if(!userConfigFile.exists()) {
                System.out.println("user custom generator config not exists, please confirm file exists : " + userConfigFile.getAbsolutePath());
            } else {
                try {
                    is = new FileInputStream(userConfigFile);
                    properties.load(is);
                } catch (Exception var13) {
                    ;
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        } finally {
            if(is != null) {
                IOUtils.closeQuietly(is);
            }

        }

        tableInfo.setGenerateDomain(Boolean.valueOf(properties.getProperty("generate.domain")).booleanValue());
        tableInfo.setGenerateSqlMap(Boolean.valueOf(properties.getProperty("generate.sqlmap")).booleanValue());
        tableInfo.setGenerateDao(Boolean.valueOf(properties.getProperty("generate.dao")).booleanValue());
        tableInfo.setGenerateManager(Boolean.valueOf(properties.getProperty("generate.manager")).booleanValue());
        tableInfo.setGenerateService(Boolean.valueOf(properties.getProperty("generate.service")).booleanValue());
        tableInfo.setGenerateController(Boolean.valueOf(properties.getProperty("generate.controller")).booleanValue());
        tableInfo.setGenerateCreate(Boolean.valueOf(properties.getProperty("generate.create")).booleanValue());
        tableInfo.setGenerateUpdate(Boolean.valueOf(properties.getProperty("generate.update")).booleanValue());
        tableInfo.setGenerateDelete(Boolean.valueOf(properties.getProperty("generate.delete")).booleanValue());
        tableInfo.setGenerateQuery(Boolean.valueOf(properties.getProperty("generate.query")).booleanValue());
        tableInfo.setOnExistsOverride(Boolean.valueOf(properties.getProperty("generate.on.exists.override")).booleanValue());
        tableInfo.setNeedSharding(Boolean.valueOf(properties.getProperty("generate.needSharding")).booleanValue());
        return tableInfo;
    }

    private static String findPackagePath(File javaSrcDir) {
        if(javaSrcDir != null && javaSrcDir.exists()) {
            LinkedList pathQueue = new LinkedList();
            File[] var5;
            int var4 = (var5 = javaSrcDir.listFiles()).length;

            for(int var3 = 0; var3 < var4; ++var3) {
                File file = var5[var3];
                if(!"domain".equals(pathQueue.peek())) {
                    travese(file, pathQueue);
                }
            }

            if(pathQueue.size() > 0) {
                pathQueue.pop();
            }

            Collections.reverse(pathQueue);
            return StringUtils.join(pathQueue, ".");
        } else {
            return "";
        }
    }

    private static void travese(File file, Deque<String> pathQueue) {
        if(file.isDirectory()) {
            pathQueue.push(file.getName());
            if(!"domain".equals(pathQueue.peek())) {
                if(notHasSubDirectory(file)) {
                    if(pathQueue.size() > 0) {
                        pathQueue.pop();
                    }

                } else {
                    File[] var5;
                    int var4 = (var5 = file.listFiles()).length;

                    for(int var3 = 0; var3 < var4; ++var3) {
                        File sub = var5[var3];
                        if(!"domain".equals(pathQueue.peek())) {
                            travese(sub, pathQueue);
                        }

                        if(!"domain".equals(pathQueue.peek()) && pathQueue.size() > 0) {
                            pathQueue.pop();
                        }
                    }

                }
            }
        }
    }

    private static boolean notHasSubDirectory(File file) {
        File[] var4;
        int var3 = (var4 = file.listFiles()).length;

        for(int var2 = 0; var2 < var3; ++var2) {
            File sub = var4[var2];
            if(sub.isDirectory()) {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        System.out.println(findPackagePath(new File("D:\\workspace\\aa\\aa-domain\\src\\main\\java")));
    }
}
